package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.caysn.autoreplyprint.AutoReplyPrint
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.DialogUtil
import com.two.screen.water.balance.utils.ToastUtil
import com.two.screen.water.balance.widget.DividerItemDecoration
import kotlinx.android.synthetic.main.activity_index.*
import java.util.concurrent.TimeUnit


class ChooseUSBPrintDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mSelectedOneDialogListener: SelectedOneDialogListener? = null
    private var mDialogGoBackLayout: View? = null
    private var mDialogDisconnectTv: TextView? = null
    private var mDialogSearchTv: TextView? = null
    private var mCommonRecyclerView: RecyclerView? = null
    lateinit var mChooseUSBPrintDialogAdapter: ChooseUSBPrintDialogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_choose_usb_print)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mDialogGoBackLayout = findViewById(R.id.mDialogGoBackLayout)
        mDialogDisconnectTv = findViewById(R.id.mDialogDisconnectTv)
        mDialogSearchTv = findViewById(R.id.mDialogSearchTv)
        mCommonRecyclerView = findViewById(R.id.mCommonRecyclerView)

        mDialogGoBackLayout?.setOnClickListener { dismiss() }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        (mCommonRecyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mHadChooseRecyclerView?.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL_LIST, 1, ContextCompat.getColor(App.context!!, R.color.line_color)))
        mChooseUSBPrintDialogAdapter = ChooseUSBPrintDialogAdapter()
        mCommonRecyclerView?.layoutManager = LinearLayoutManager(mContext)
        mCommonRecyclerView?.adapter = mChooseUSBPrintDialogAdapter
        mChooseUSBPrintDialogAdapter.bindToRecyclerView(mCommonRecyclerView)
        mChooseUSBPrintDialogAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty1)
    }

    /*商品列表 适配器*/
    inner class ChooseUSBPrintDialogAdapter : BaseQuickAdapter<SelectedBean, BaseViewHolder>(R.layout.item_dialog_usb_printer, null) {
        @SuppressLint("CheckResult", "NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: SelectedBean) {
            helper
                .setText(R.id.item_tv1, item.name)
                .getView<TextView>(R.id.item_tv2).apply {
                    setOnClickListener {

                    }
                }
        }
    }

    fun setListData() {
        val enumUsbList = AutoReplyPrint.CP_Port_EnumUsb_Helper.EnumUsb()?.map { SelectedBean(null,it) }
        mChooseUSBPrintDialogAdapter.setNewData(enumUsbList)
        mDialogSearchTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    ToastUtil.showToast("开始搜索USB下票打印...")
                }
        }
        mDialogDisconnectTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    ToastUtil.showToast("断开连接成功!")
                }
        }

    }

    fun setSelectedOneDialogListener(l: SelectedOneDialogListener) {
        mSelectedOneDialogListener = l
    }


    interface SelectedOneDialogListener {
        fun onSelected(item: SelectedBean)
    }
    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
